package com.fs.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fs.dao.UserInfoDao;
@WebServlet("/register.do")
public class UserRegisterServlet extends HttpServlet{
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
			doPost(req, resp);
		}
		
		
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			req.setCharacterEncoding("utf-8");//设置请求编码格式
			resp.setContentType("text/html;charset=utf-8");//设置响应的编码格式
			resp.setCharacterEncoding("utf-8");
			PrintWriter out = resp.getWriter();
			String user_email = req.getParameter("user_email");
			String user_password = req.getParameter("user_password");
			String usertype = req.getParameter("usertype");
			UserInfoDao userDao = new UserInfoDao();
			boolean flag = userDao.IsexitEmailUsername(user_email);
			if(flag){
				//邮箱已经注册
				out.print("<script type ='text/javascript'>");
				out.print("alert('邮箱已被注册，请重新输入');");
				out.print("window.location.href='Register.jsp';");
				out.print("</script>");
			}else{
				//保存注册信息
				out.print("<script type ='text/javascript'>");
				out.print("alert('恭喜您注册成功，快去登录吧！');");
				out.print("window.location.href='Login.jsp';");
				out.print("</script>");
				userDao.save(user_email,user_password,usertype);
			}
		}
}
